<template>
	<view class="page">
		<view class="list" style="border-bottom: 1rpx solid #eee;">
			<view class="list__title">标题</view>
			<view class="list__input">
				<input type="text" v-model="data.label" placeholder="请输入标题" placeholder-style="font-size: 35rpx;">
			</view>
		</view>
		<view class="options">
			<view class="options__head">选项</view>
			<view class="options__content">
				<sortable-renderjs domId="horizontal" @changeList="horizontalListChange" :options="options">
					<view class="list1 horizontal" id="horizontal">
						<view class="list1__item" v-for="(item,index) in data.list" :key="item.value" :data-id="item.value" @longtap="longpressModule">
							<view class="list1__item--icon" @click="del(item,index)">
								<image src="@/static/theme/default/home/henggang.png" mode=""></image>
							</view>
							<input class="list1__item--title" v-model="item.name" type="text" v-if="item.value != -1"></input>
							<view class="list1__item--title" style="color: #aaa;" v-else>其他</view>
							<view class="list1__item--sort">
								<image src="@/static/theme/default/community/list.png" mode="" v-if="item.value != -1"></image>
							</view>
						</view>
					</view>
				</sortable-renderjs>
				<view class="list1__item" @click="add">
					<view class="list1__item--icon">
						<image src="@/static/theme/default/home/add1.png" mode=""></image>
					</view>
					<view class="list1__item--title">添加选项</view>
					<view class="list1__item--sort"></view>
				</view>
			</view>
		</view>
		
		<view class="option">
			<view class="option__left">可输入其他选项</view>
			<view class="option__right">
				<u-switch v-model="inputOther" @change="change"></u-switch>
			</view>
		</view>
		<view class="option">
			<view class="option__left">必填</view>
			<view class="option__right">
				<u-switch v-model="data.required"></u-switch>
			</view>
		</view>
		
		<view class="del" @click="remove">删除</view>
		
		<view class="confirm">
			<u-button type="primary" color="#2979ff" text="完成" @click="confirm"></u-button>
		</view>
	</view>
</template>

<script>
	import {sendBackFunction} from '@/common/custom_util.js';
	import sortableRenderjs from '@/uni_modules/cms-sortable-renderjs/cms-sortable-renderjs';
	
	export default {
		components: {
			sortableRenderjs
		},
		data() {
			return {
				data: {
					label: '',
					placeholder: '请输入',
					required: false,
					list: [
						{
							value: 1,
							name: "选项1",
						},
						{
							value: 2,
							name: "选项2",
						},
						{
							value: 3,
							name: "选项3",
						},
					],
				},
				inputOther: false,
				options: {
					disabled: false,
					delay: 0
				},
			}
		},
		onLoad(options) {
			options.data ? this.data = JSON.parse(options.data) : '';
		},
		methods: {
			confirm() {
				sendBackFunction('getCheckbox',this.data)
			},
			/**
			 * 拖动事件
			 * @param {Object} v 回调参数
			 */
			horizontalListChange(v) {
				let temp = [];
				for(let i=0;i< v.length;i++) {
					let flag = false;
					let obj = {}
					for(let j = 0;j < this.data.list.length;j++) {
						if(this.data.list[j].value == v[i]) {
							flag = true;
							obj = this.data.list[j]
						}
					}
					if(flag) temp.push(obj)
				}
				this.data.list = temp;
			},
			/**
			 * 长按事件
			 */
			longpressModule() {
				uni.vibrateShort({
					success: function() {
						console.log('success');
					}
				});
			},
			del(v,i) {
				let that = this;
				uni.showModal({
					title: '是否删除?',
					content: '',
					success(res) {
						if(res.confirm) {
							if(v.value == -1) {
								that.inputOther = false;
							}
							that.data.list.splice(i,1);
						}
					}
				})
			},
			add() {
				let max = 0;
				this.data.list.forEach((item,index) => {
					max = item.value > max ? item.value : max;
				})
				let value = max + 1;
				let index = this.data.list.length + 1;
				this.data.list.push({
					value,
					label: "选项" + index,
				})
			},
			/**
			 * 切换其他选项
			 * @param {Object} v
			 */
			change(v) {
				if(v) {
					this.data.list.push({
						name: '其他',
						value: -1,
						data: ''
					})
				} else {
					let i = 0;
					this.data.list.forEach((item,index) => {
						if(item.value == -1) {
							i = index
						}
					})
					this.data.list.splice(i,1)
				}
			},
			remove() {
				let that = this;
				uni.showModal({
					title: '是否删除?',
					content: '',
					success(res) {
						if(res.confirm) {
							sendBackFunction('remove',that.data)
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.page {
		padding-bottom: 200rpx;
	}
	
	.list {
		padding: 20rpx;
		background-color: #fff;
		
		&__title {
			color: #aaa;
			font-size: 27rpx;
		}
		
		&__input {
			margin: 10rpx 0;
		}
	}
	
	.option {
		padding: 20rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 30rpx;
		
		&__left {
			font-size: 35rpx;
		}
	}
	
	.del {
		height: 100rpx;
		background-color: #fff;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 100rpx;
		color: #ff7875;
	}
	
	.confirm {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 100rpx;
		z-index: 9;
		background-color: #fff;
		padding: 20rpx;
		box-shadow: 0 -3rpx 20rpx 1rpx rgba(0, 0, 0, .1);
	}
	
	.options {
		
		&__head {
			height: 100rpx;
			display: flex;
			align-items: center;
			padding-left: 20rpx;
		}
		
		&__content {
			background-color: #fff;
		}
	}
	
	.list1 {
		background-color: #fff;
		
		&__item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1rpx solid #eee;
			
			&--icon {
				width: 100rpx;
				height: 100rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				
				image {
					width: 40rpx;
					height: 40rpx;
				}
			}
			
			&--title{
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				flex: 1;
			}
			
			&--sort {
				width: 100rpx;
				height: 100rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				
				image {
					width: 40rpx;
					height: 40rpx;
				}
			}
		}
	}
</style>
